from ClassificationTree import ClassificationTree
from RegressionTree import RegressionTree
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from sklearn.metrics import r2_score
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
import numpy as np

#西瓜数据集
WM=np.loadtxt('./decision_tree/watermelon.txt',skiprows=1,delimiter=' ')
CT = ClassificationTree(2,prun=False)
X = WM[:,:2]
y = np.array(WM[:,2],int)
CT.fit(X,y)
X,Y=np.meshgrid(np.linspace(0,1,40),np.linspace(0,0.5,40))
X = np.reshape(X,[1600,1])
Y = np.reshape(Y,[1600,1])
y_p = CT.predict(np.append(X,Y,axis=1))
plt.title('watermelon_3a')
plt.xlabel('density')
plt.ylabel('ratio sugar')
plt.scatter(X[y_p==1],Y[y_p==1],marker ='.', color = 'y', s = 10,label= 'predict as good')
plt.scatter(X[y_p==0],Y[y_p==0],marker ='.', color = 'c', s = 10,label= 'predict as bad')
plt.scatter(WM[y==1,0],WM[y==1,1], marker ='+', color = 'r', s = 100,label= 'good')
plt.scatter(WM[y==0,0],WM[y==0,1], marker ='_',color = 'b',s = 100,label = 'bad')
plt.legend(loc= 'upper right')
plt.show()
CT.show(['密度','含糖率'],['坏瓜','好瓜'])

# 鸢尾花数据集
'''
iris = datasets.load_iris()
X = iris.data
y = iris.target
X_train,X_test,y_train,y_test =train_test_split(X,y,stratify=y)
CT = ClassificationTree(3,prun=True,postprun=True)
CT.fit(X_train,y_train)
CT.show(iris['feature_names'],iris['target_names'])
print(classification_report(y_test,CT.predict(X_test)))
'''

#乳腺癌数据集
'''
breast_cancer = datasets.load_breast_cancer()
CT = ClassificationTree(2,prun=True,postprun=False)
X_train,X_test,y_train,y_test = train_test_split(breast_cancer.data,breast_cancer.target,stratify=breast_cancer.target)
CT.fit(X_train,y_train)
CT.show(breast_cancer.feature_names,breast_cancer.target_names)
print(classification_report(y_test,CT.predict(X_test)))
'''

#糖尿病数据集
'''
diabetes = datasets.load_diabetes()
X_train,X_test,y_train,y_test = train_test_split(diabetes.data,diabetes.target)
RT = RegressionTree(maxdepth=None,prun=True,postprun=True)
RT.fit(X_train,y_train)
RT.show(diabetes.feature_names,['diabete'])
print("r2 in train set: %.4f"%r2_score(y_train,RT.predict(X_train)))
print("r2 in test set: %.4f"%r2_score(y_test,RT.predict(X_test)))
'''

#加利福利亚房价数据集
'''
california = datasets.fetch_california_housing()
x_train,x_test,y_train,y_test = train_test_split(california.data[:1000,0],california.target[:1000])
RT = RegressionTree(maxdepth=None,prun=True,postprun=False)
RT.fit(np.reshape(x_train,[-1,1]),y_train)
# RT.show(california.feature_names,california.target_names)
print("r2 in train set: %.4f"%r2_score(y_train,RT.predict(np.reshape(x_train,[-1,1]))))
y_p = RT.predict(np.reshape(x_test,[-1,1]))
print("r2 in test set: %.4f"%r2_score(y_test,y_p))
plt.title('California Housing')
plt.xlabel('MedInc')
plt.ylabel('MedHouseVal')
plt.scatter(x_test,y_test,marker='.',c='r',s=10)
x = np.linspace(0,15,1000)
y = RT.predict(x.reshape([-1,1]))
plt.plot(x,y,c='b')
plt.show()
'''